package com.senseuni.da.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.senseuni.da.R;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private final Context mContext;
    private List<String> mDataList;

    public MyAdapter(Context context) {
        this.mContext = context;
        mDataList = new ArrayList<>();
    }

    public void setDataList(List<String> list) {
        mDataList = list;
    }

    public void addItem(String text) {
        mDataList.add(text);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_list, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String text = mDataList.get(position);
        holder.mContentView.setText(text);
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView mContentView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            mContentView = itemView.findViewById(R.id.textView);
        }
    }
}
